﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace XTools.App_Code
{
    public class StationOpera
    {
        //设置文件路径
        public static void SetStationPath(string file_path)
        {
            Dll.SetStationPath(file_path);
        }

        //打开文件
        public static string OpenCfgs(string stations_path)
        {
            IntPtr pData = Dll.OpenCfgs(stations_path);
            return Marshal.PtrToStringAuto(pData);
        }

        //读取站点
        public static string ReadStation(string station_id, string cfgs)
        {
            IntPtr pData = Dll.ReadStation(station_id, cfgs);
            return Marshal.PtrToStringAuto(pData);
        }

        //删除站点
        public static string DeleteStation(string station_id, string cfgs)
        {
            IntPtr pData = Dll.DeleteStation(station_id, cfgs);
            return Marshal.PtrToStringAuto(pData);
        }

        //新增站点
        public static string InsertStation(string station_cfg, string cfgs)
        {
            IntPtr pData = Dll.InsertStation(station_cfg, cfgs);
            return Marshal.PtrToStringAuto(pData);
        }

        //关闭保存文件
        public static bool CloseCfgs(string station_cfg_file, string cfgs)
        {
            return Dll.CloseCfgs(station_cfg_file, cfgs);
        }

        //判断站点是否存在
        public static bool IsStaIDExist(string StaID, string cfgs)
        {
            return Dll.IsStaIDExist(StaID, cfgs);
        }

        //获取基元、虚元数
        public static string GetStationTableNum(string StaID, string cfgs)
        {
            IntPtr pData = Dll.GetStationTableNum(StaID, cfgs);
            return Marshal.PtrToStringAuto(pData);
        }

        //读取属性
        public static string ReadCfg(string para_id, string station_cfg)
        {
            IntPtr pData = Dll.ReadCfg(para_id, station_cfg);
            return Marshal.PtrToStringAuto(pData);
        }

        //批量读取属性
        public static string ReadCfgList(string para_id, string pos_list, string station_cfg)
        {
            IntPtr pData = Dll.ReadCfgList(para_id, pos_list, station_cfg);
            return Marshal.PtrToStringAuto(pData);
        }

        //写入属性
        public static string WriteCfg(string sReplaceBy, string para_id, string station_cfg)
        {
            IntPtr pData = Dll.WriteCfg(sReplaceBy, para_id, station_cfg);
            return Marshal.PtrToStringAuto(pData);
        }

        //读取站点名称、站点编号
        public static string GetCfgs(string station_cfg_file)
        {
            IntPtr pData = Dll.GetCfgs(station_cfg_file);
            string str = Marshal.PtrToStringAuto(pData);
            if (str == null)
            {
                return "";
            }
            else
            {
                return str;
            }
        }

        //反转数据 "低位在前 高位在后"
        public static string ReviseStation(string station_cfg)
        {
            IntPtr pData = Dll.ReviseStation(station_cfg);
            return Marshal.PtrToStringAuto(pData);
        }

      
        /***************MIXIO*******************/
        //获取文件名
        public static string GetFieldName(string szData, string szTable)
        {
            IntPtr pData = Dll.GetFieldName(szData, szTable);
            return Marshal.PtrToStringAuto(pData);
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="sFieldID"></param>
        /// <param name="szStation"></param>
        /// <param name="szTable"></param>
        /// <returns></returns>
        public static string ReadCfgEx(string sFieldID, string szStation, string szTable)
        {
            IntPtr pData = Dll.ReadCfgEx(sFieldID, szStation,szTable);
            return Marshal.PtrToStringAuto(pData);
        }

        public static string m_stations = "";
        public static string m_station = "";
        public static string GetDecodeExStation(string str)
        {
            string result = string.Empty;
            result = str.Replace("{", "");
            result = result.Replace("}", "");
            result = result.Replace("\r\n", "");
            return result;
        }
        public static bool OpenStation(string sta_ID)
        {
            m_stations = OpenCfgs("stations.ini");
            if (m_stations.Trim() == "")
            {
                return false;
            }
            m_station = ReadStation(sta_ID, m_stations);
            if (m_stations.Trim() == "")
            {
                return false;
            }
            return true;
        }

        public static string ReadConfig(string para_id)
        {
            return ReadCfg(para_id, m_station);
        }

        public static string ReadCfgList(string para_id, string pos_list)
        {
            return ReadCfgList(para_id, pos_list, m_station);
        }

        public static int GetStationTableNum(string StaID)
        {
            string strNum = GetStationTableNum(StaID, m_station);
            try
            {
                return int.Parse(strNum);
            }
            catch (Exception)
            {
                return -1;
            }
        }


    }
}